package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.CommentMapper;
import edu.dayu.pojo.Comment;
import edu.dayu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;


    @Override
    public List<Comment> listComment(Comment comment) {
        List<Comment> comments = commentMapper.listComment(comment);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment1 : comments) {
            replyChildren(comment1);
            comment1.setCommentList(replyComment);
            commentList.add(comment1);
           replyComment=new ArrayList<>();
        }
        return commentList;
    }


    List<Comment> replyComment = new ArrayList<>();
    /**
     * 利用递归获得评论的所有子集
     * @param comment
     */
    public void replyChildren(Comment comment){
        Comment comment1 = new Comment();
        comment1.setBlog_id(comment.getBlog_id());
        comment1.setParent_id(comment.getId());
        List<Comment> comments = commentMapper.listComment(comment1);
        if (comments.size()>0){
            for (Comment comment2 : comments) {
                comment2.setUsername(comment.getNickname());
                replyComment.add(comment2);
                replyChildren(comment2);
            }
        }
    }

    @Override
    public Boolean insertComment(Comment comment) {
        comment.setCreateTime(new Date());
        return commentMapper.insertComment(comment);
    }
}
